// ValidationAspects
// Written by Mike Saunders
// Released under Microsoft Public License (Ms-PL)
using System;
using System.Reflection;
using ValidationAspects.Sdk;

namespace ValidationAspects.Core
{
	public class ValidationContext : IValidationContext
	{
		private readonly object _instance;
		private readonly Type _type;
		private readonly PropertyInfo _property;
		private readonly MethodBase _method;
		private readonly ParameterInfo _parameter;
		private readonly object[] _arguments;

		public ValidationContext(object instance, Type type, PropertyInfo property, MethodBase method, ParameterInfo parameter, object[] methodArgs)
		{
			Enforce.IsNotNull(type, "type");

			_instance = instance;
			_type = type;
			_property = property;
			_method = method;
			_parameter = parameter;
			_arguments = methodArgs;
		}

		public ValidationContext(object instance, PropertyInfo property, object value)
		{
			Enforce.IsNotNull(property, "property");

			_instance = instance;
			_type = property.DeclaringType;
			_property = property;
			_method = property.GetSetMethod(true);
			_parameter = _method.GetParameters()[0];
			_arguments = new[] {value};
		}

		public object Instance
		{
			get { return _instance; }
		}

		public Type Type
		{
			get { return _type; }
		}

		public PropertyInfo Property
		{
			get { return _property; }
		}

		public MethodBase Method
		{
			get { return _method; }
		}

		public ParameterInfo Parameter
		{
			get { return _parameter; }
		}

		public object[] Arguments
		{
			get { return _arguments; }
		}
	}
}